package com.ocom.payconfig.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ocom.common.entity.human.EntityDeptInfo;
import com.ocom.common.entity.human.EntityPersonTagRelation;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoPersonTagRelation;
import com.ocom.payconfig.service.PersonTagRelationService;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.stereotype.Service;

@Service
public class PersonTagRelationServiceImpl extends JoinServiceImpl<IDaoPersonTagRelation, EntityPersonTagRelation> implements PersonTagRelationService {



    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE )
    public Page<EntityPersonTagRelation> getByComId(Long companyId,Integer tagId ,Integer page ,Integer size) {
        JoinLambdaWrapper<EntityPersonTagRelation> queryWrapper = new JoinLambdaWrapper<>(EntityPersonTagRelation.class);
        queryWrapper.eq(EntityPersonTagRelation::getComId, companyId);
        queryWrapper.eq(EntityPersonTagRelation::getTagId, tagId);
        queryWrapper.leftJoin(EntityPersonnelInfo.class,  EntityPersonnelInfo::getPId,EntityPersonTagRelation::getPId, "personnel_info")
                .eq(EntityPersonnelInfo::getStatus,0)
                .selectAs((cb) -> {
                    cb.add(" personnel_info.dept_id ","deptId",false);
                    cb.add(" personnel_info.p_name ","pName",false);
                    cb.add(" personnel_info.p_code ","pCode",false);
                })
                .end();
        queryWrapper.leftJoin(EntityDeptInfo.class,  EntityDeptInfo::getDeptId,EntityPersonnelInfo::getDeptId, "dept_info")
                .selectAs((cb) -> {
                    cb.add(" dept_info.dept_name ","deptName",false);
                })
                .end();
        Page<EntityPersonTagRelation> page1 = new Page<>(page, size);
        return this.joinPage(page1,queryWrapper, EntityPersonTagRelation.class);
    }


}
